package com.api.phm.domain.query.alarmRule;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.api.common.config.ApiConfig;
import com.api.common.utils.DateUtils;
import com.api.phm.domain.model.AlaRuleDevModel;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Getter
@Setter
public class AlarmRuleQuery {
    private Long plcId;
    private JSONObject localPlcName;
    private Long lineId;
    private JSONObject localLineName;
    private Long robotId;
    private JSONObject localRobotName;
    private String ruleType;
    private Long ruleId;
    private JSONObject localRuleName;
    private String ruleRepeat;
    private String alarmMessage;
    private String restoreMessage;
    private JSONObject deviceMap;
    @JsonIgnore
    private Map<String, AlaRuleDevModel> alaRuleDevModelMap;
    private JSONArray rules;
    private String strRules;
    private List<Long> contacts;
    private Long delayTime;
    private String enable;
    /**
     * 描述
     */
    private String remark;
    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    /**
     * 创建人
     */
    private String createBy;
    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;
    /**
     * 更新人
     */
    private String updateBy;

    public void setContacts(String strContacts) {
        if (StringUtils.isNotBlank(strContacts)) {
            this.contacts = JSONArray.parseArray(strContacts, Long.class);
        }
    }

//    public void setLocalRuleName(JSONObject localRuleName) {
//        this.localRuleName = localRuleName;
//    }

    public void setLocalRuleName(String localRuleName) {
        this.localRuleName = JSONObject.parseObject(localRuleName);
    }

    public void setLocalPlcName(String localPlcName) {
        this.localPlcName = JSONObject.parseObject(localPlcName);
    }

    public void setLocalLineName(String localLineName) {
        this.localLineName = JSONObject.parseObject(localLineName);
    }

    public void setLocalRobotName(String localRobotName) {
        this.localRobotName = JSONObject.parseObject(localRobotName);
    }

    public void setDeviceMap(String strDeviceMap) {
        if (StringUtils.isNotBlank(strDeviceMap)) {
            JSONObject jo = JSONObject.parseObject(strDeviceMap);
            this.deviceMap = jo;
            Map<String, AlaRuleDevModel> map = new HashMap<>();
            for (String key : jo.keySet()) {
                JSONObject subJo = jo.getJSONObject(key);
                if (StringUtils.isNotBlank(subJo.getString("key"))) {
                    map.put(key, new AlaRuleDevModel(subJo.getLong("plcId")
                            , subJo.getLong("robotId")
                            , subJo.getLong("elementId")
                            , subJo.getString("datCol"))
                    );
                } else {
                    map.put(key, new AlaRuleDevModel(subJo.getString("key")));
                }
            }
            this.alaRuleDevModelMap = map;
        }
    }

    public void setRules(String rules) {
        System.out.println(rules);
        this.strRules = rules;
        this.rules = JSONArray.parseArray(rules);
        System.out.println(this.rules);
    }

    /**
     * 制作规则信息
     *
     * @param level
     * @param param
     * @return
     */
    public String mkMessage(int level, JSONObject param) {
        String message = level == 0 ? this.restoreMessage : this.alarmMessage;
        String strDateTime = DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD_HH_MM_SS);
        Map<String, String> valuesMap = new HashMap<>();
        valuesMap.put("ruleName", this.localRuleName.getString(ApiConfig.getInitLang()));
        valuesMap.put("now", strDateTime);
        valuesMap.put("level", level + "");
        valuesMap.put("val", param.toString());
        StringSubstitutor sub = new StringSubstitutor(valuesMap);
        if (StringUtils.isNotBlank(message)) {
            return sub.replace(message);
        }
        return "";
    }
}
